package com.deyuanyun.pic.pipe.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.pipe.controller.dto.PipeBaseQuery;
import com.deyuanyun.pic.pipe.controller.vo.ClosureVO;
import com.deyuanyun.pic.pipe.enumeration.PipeExportEnum;
import com.deyuanyun.pic.pipe.service.PipeMgrService;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * “封堵物”控制类
 *
 * @ClassName ClosureController
 * @author 荣华兴
 * @date 2015年8月13日
 */
@Controller
@RequestMapping("/pipe/closure")
public class ClosureController extends PipeBaseController {

	@Resource
	private PipeMgrService pipeMgrService;

	/**
	 * 跳转到 封堵物 主页面
	 *
	 * @return
     */
	@RequestMapping("/toClosure")
	public ModelAndView toClosureMgr() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/basedata/pipe/closureMgr");
		return mav;
	}

	/**
	 * 根据条件分页查询 封堵物 数组对象
	 *
	 * @param pipeBaseQuery
	 */
	@RequestMapping("/query.json")
	@ResponseBody
	public void query(PipeBaseQuery pipeBaseQuery) {
		PageJsonBean jsonBean = new PageJsonBean();
		jsonBean.setList(this.queryVO(pipeBaseQuery));
		AjaxSupport.sendSuccessText("success", jsonBean);
	}

	/**
	 * 根据条件导出 封堵物
	 *
	 * @param pipeBaseQuery
	 * @throws IOException
	 */
	@RequestMapping("/export.json")
	@ResponseBody
	public void export(PipeBaseQuery pipeBaseQuery) throws IOException {
		List list = this.queryVO(pipeBaseQuery);
		PipeExportEnum exportEnum = PipeExportEnum.Closure;
		String[] columnKeys = {"pipeLineName", "customNumber", "closureType", "closureMaterial", "mileage", "reference",
				"referenceDistance", "locationDescription", "antisepticMaterial", "pressureRating", "inserviceDate",
				"assemblyCompany", "inspectingCompany","testingCompany", "recorder", "recordTime", "remark"};
		DownExcelUtil.export(exportEnum.getFileName(), pipePath+exportEnum.getTempPath(), exportEnum.getBeginRow(), list, columnKeys);
	}

	private List queryVO(PipeBaseQuery pipeBaseQuery) {
		return new ClosureVO().fromDomainList(pipeMgrService.queryClosure(pipeBaseQuery));
	}
}
